﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace RH.EBS.Core.Repository.DBUtility
{
    /// <summary>
    /// 此类负责解析Ora-Code的中文代码。
    /// </summary>
    public class SqlCodeParser
    {
        //Oracle保留关键字
        private static List<string> _ReserveKeys;

        static SqlCodeParser()
        {
            _ReserveKeys = new List<string>();

            _ReserveKeys.Add("LEVEL");
            _ReserveKeys.Add("CYCLE");
            _ReserveKeys.Add("OPERATOR");
            _ReserveKeys.Add("SECTION");
        }

        public static bool IsReserveKey(string key)
        {
            return _ReserveKeys.Contains(key.ToUpper());
        }

        /// <summary>
        /// 解析异常，返回中文描述。
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        public static string Parse(SqlException ex)
        {
            LogWriter.Write(LOG_CATEGORY.SYSTEM, LOG_LEVEL.ERROR, ex.ToString());

            switch (ex.ErrorCode)
            {
                case 2292:
                    return "违反完整约束条件！";
                case 1036:
                    return "非法的变量名/编号！";
                case 903:
                    return "表名无效！";
                case 904:
                    return "标识符无效！";
            }

            return "Ora-" + ex.ErrorCode.ToString();
        }
    }
}
